package com.yyfax.ph.home.recommendloan.basicinfo;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.secure.RSA;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.GlobalVariableConfig;
import com.yyfax.ph.personal.authority.RSAPresenter;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CheckInBasicInfoPresenter extends RSAPresenter<CheckInBasicInfoModel, CheckInBasicInfoPresenter.View> {

	public static final String HAS_CREDIT_PAPERS = "1"; // 有纸质征信报告
	public static final String NO_CREDIT_PAPERS = "0"; // 无纸质征信报告
	private static final String NOT_SUPPORT_CODE = "10023"; // 有无通道是否已开通允许

	private static final String ADMITTANCE_FAILED = "准入失败";
	private static final String UPLOAD_FAILED = "上传失败";
	private static final String NOT_SUPPORT = "暂不支持";


	public static final String TAG = CheckInBasicInfoPresenter.class.getSimpleName();

	public interface View extends IView {

		void checkInBasicInfo(CheckInBasicInfoModel.CheckBasicInfoBean checkBasicInfoBean);

		void checkInBasicInfoFailure(String tips);

		void checkAdmittanceAlert(String tips);

		void checkAdmittanceConfirm(String tips);

		void checkAdmittanceOk();

		void checkAdmittanceError(String tips);
	}

	public static class Generator implements PresenterFactory<CheckInBasicInfoPresenter> {
		@Override
		public CheckInBasicInfoPresenter create() {
			return new CheckInBasicInfoPresenter();
		}
	}

	private View mView;

	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new CheckInBasicInfoModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Override
	public Object getRSAParams(Object params, int functionalCode) {
		String keyRSA = GlobalVariableConfig.getKeyRSA();
		switch (functionalCode) {
			case CheckInBasicInfoModel.MSG_CHECK_ADMITTANCE:
				CheckInBasicInfoModel.Admittance admittance = (CheckInBasicInfoModel.Admittance) params;
				admittance.certId = RSA.encryptByPublicKey(admittance.certId, keyRSA);
				return admittance;
			case CheckInBasicInfoModel.MSG_CHECK_BASIC_INFO:
				CheckInBasicInfoModel.BasicInfo basic = (CheckInBasicInfoModel.BasicInfo) params;
				basic.certId = RSA.encryptByPublicKey(basic.certId, keyRSA);
				basic.phone = RSA.encryptByPublicKey(basic.phone, keyRSA);
				return basic;
		}
		return params;
	}

	public void checkAdmittance(String certId, String name) {
		CheckInBasicInfoModel.Admittance admittance = new CheckInBasicInfoModel.Admittance();
		admittance.certId = certId.toUpperCase();
		admittance.name = name;
		execute(admittance, CheckInBasicInfoModel.MSG_CHECK_ADMITTANCE, false, null, mRequestPage);// TODO: 2017/3/7 重复提交判断
	}

	public void checkInBasicInfo(CheckInBasicInfoModel.BasicInfo basicInfo) {
		basicInfo.certId = basicInfo.certId.toUpperCase();
		execute(basicInfo, CheckInBasicInfoModel.MSG_CHECK_BASIC_INFO, true, null,mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void checkAdmittanceResult(CheckInBasicInfoModel.AdmittanceResult result) {// TODO: 2017/3/8 确认alert和confirm的情况
		if (mRequestPage.equals(result.requestPage)) {
			CLog.i(TAG, "rs=" + result.toString());
			if (isResponseSuccess(result)) {
				mView.checkAdmittanceOk();
			} else if (isResponseWarning(result)) {//todo 在父类判断
				mView.checkAdmittanceAlert(TextUtils.isEmpty(result.getMsg()) ? ADMITTANCE_FAILED : result.getMsg());//10004:贷款不符合;10005: 贷款不符合，年龄不满足
			} else {
				mView.checkAdmittanceError(TextUtils.isEmpty(result.getMsg()) ? ADMITTANCE_FAILED : result.getMsg());
			}
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void checkInResult(CheckInBasicInfoModel.BasicInfoResult result) {
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_B003, result.getStatus());
		if (mRequestPage.equals(result.requestPage)) {
			CLog.i(TAG, "rs=" + result.toString());
			if (isResponseSuccess(result)) {
				mView.checkInBasicInfo(result.getContent());
			} else {
				String msg = UPLOAD_FAILED;
				if (isResponseWarning(result) && result.getContent() != null && TextUtils.equals(NOT_SUPPORT_CODE,result.getContent().code)){
					msg = NOT_SUPPORT;
				}

				if (TextUtils.isEmpty(result.getMsg())) {
					mView.checkInBasicInfoFailure(msg);
				} else {
					mView.checkInBasicInfoFailure(result.getMsg());
				}
			}
		}
	}
}
